﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

//爬虫
namespace Crawler
{
    public partial class Form1 : Form
    {
        //储存网址
        string URL = "";
        //储存正则表达式
        string PhoneMatchRule = @"(1[3456789]\d{9})|(6877\d{4})";
        string EmailMatchRule = @"[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,14}";
        string NewMatchRule;
        public Form1()
        {
            InitializeComponent();
        }
      
        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button2_Click_1(object sender, EventArgs e)
        {
            URL = richTextBox1.Text;
            try
            {
                //匹配电话和邮箱
                WebClient webClient = new WebClient();
                byte[] pageData = webClient.DownloadData(URL);
                string pageHtml = Encoding.UTF8.GetString(pageData);
                MatchCollection emailMatches = Regex.Matches(pageHtml, EmailMatchRule);
                MatchCollection phoneMatches = Regex.Matches(pageHtml, PhoneMatchRule);
                StringBuilder stringBuilder = new StringBuilder();
                foreach (var email in emailMatches)
                {
                    stringBuilder.Append("email:" + email + "\n");
                }
                stringBuilder.Append("\n");
                foreach (var phone in phoneMatches)
                {
                    stringBuilder.Append("phone:" + phone + "\n");
                }
                string result = stringBuilder.ToString();
                stringBuilder.Clear();
                button1.Text = result;
            }
            catch (Exception)
            {
                button1.Text = "Please check the network connection or enter the website address";
            }

        }

        private void button3_Click(object sender, EventArgs e)
        {
            URL = richTextBox1.Text;
            NewMatchRule = richTextBox2.Text;
            try
            {
                WebClient webClient = new WebClient();
                byte[] pageData = webClient.DownloadData(URL);
                string pageHtml = Encoding.UTF8.GetString(pageData);
                MatchCollection newMatches = Regex.Matches(pageHtml, @NewMatchRule);
                StringBuilder newSB = new StringBuilder();
                foreach (var newItem in newMatches)
                {
                    newSB.Append("Query Content：" + newItem + "\n");
                }
                string result1 = newSB.ToString();
                newSB.Clear();
                button1.Text = result1;
            }
            catch (Exception)
            {
                button1.Text = "Please check network connection or input";
                throw;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {

        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }
    }
}
